import { AmmoLib } from '../AmmoLib.js'
import { WuLiGangTi } from './WuLiGangTi.js'

class WuLiChe extends WuLiGangTi {
	wuTiLeiXing = '车'
	che = null // 车
	chiCun = null // 底盘尺寸
	tuning = null
	vehicleSteering = 0
	
	constructor(canShu, shiJie) {
		super(canShu, shiJie)
		
		this.chiCun = canShu.pengZhuangTi.zi[0].chiCun
		
		this.che = this.shengChengChe()
		this.shiJie.addAction(this.che)
		this.tianJiaLunZi()
	}
	
	// 生成车
	shengChengChe() {
		this.tuning = new AmmoLib.btVehicleTuning();
		let rayCaster = new AmmoLib.btDefaultVehicleRaycaster(this.shiJie);
		let vehicle = new AmmoLib.btRaycastVehicle(this.tuning, this.wuTi, rayCaster);
		vehicle.setCoordinateSystem(0, 1, 2)
		
		return vehicle
	}
	// 添加轮子
	tianJiaLunZi() {
		let { width, height, depth } = this.chiCun
		
		var wheelRadiusFront = height * 2;
		var wheelWidthFront = height * 1.5;
		var wheelHalfTrackFront = width / 2 + wheelWidthFront / 2;
		var wheelAxisHeightFront = height; // wheelDirectionCS0.y的位置为原点
		var wheelAxisFrontPosition = depth * 0.4;
		
		var wheelRadiusBack = height * 2;
		var wheelWidthBack = height * 1.5;
		var wheelHalfTrackBack = width / 2 + wheelWidthBack / 2;
		var wheelAxisHeightBack = height;
		var wheelAxisPositionBack = -depth * 0.4;

		var friction = 1000;
		var suspensionStiffness = 20.0;
		var suspensionDamping = 2.3;
		var suspensionCompression = 4.4;
		var suspensionRestLength = 0.6;
		var rollInfluence = 0.2;

		var wheelDirectionCS0 = new AmmoLib.btVector3(0, -1, 0); // 轮子转向轴
		var wheelAxleCS = new AmmoLib.btVector3(-1, 0, 0); // 轮子轴的方向

		let addWheel = (pos, radius, width, isFront) => {

			let wheelInfo = this.che.addWheel( pos, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, radius, this.tuning, isFront);

			wheelInfo.set_m_suspensionStiffness(suspensionStiffness);
			wheelInfo.set_m_wheelsDampingRelaxation(suspensionDamping);
			wheelInfo.set_m_wheelsDampingCompression(suspensionCompression);
			wheelInfo.set_m_frictionSlip(friction);
			wheelInfo.set_m_rollInfluence(rollInfluence);
		}

		addWheel(new AmmoLib.btVector3(wheelHalfTrackFront, wheelAxisHeightFront, wheelAxisFrontPosition), wheelRadiusFront, wheelWidthFront, true);
		addWheel(new AmmoLib.btVector3(-wheelHalfTrackFront, wheelAxisHeightFront, wheelAxisFrontPosition), wheelRadiusFront, wheelWidthFront, true);
		addWheel(new AmmoLib.btVector3(wheelHalfTrackBack, wheelAxisHeightBack, wheelAxisPositionBack), wheelRadiusBack, wheelWidthBack, false);
		addWheel(new AmmoLib.btVector3(-wheelHalfTrackBack, wheelAxisHeightBack, wheelAxisPositionBack), wheelRadiusBack, wheelWidthBack, false);
	}
	// 设置车
	sheZhiWuTi(canShu) {
		super.sheZhiWuTi(canShu)
		
		if (!canShu.sheZhiChe) return

		let vehicle = this.che
		let vehicleSteering = this.vehicleSteering
		let speed = vehicle.getCurrentSpeedKmHour()
		let breakingForce = 0;
		let engineForce = 0;
		var steeringIncrement = 0.1 ; // .04
		var steeringClamp = .5;
		var maxEngineForce = 2000;
		var maxBreakingForce = 100;
		let FRONT_LEFT = 0, FRONT_RIGHT = 1, BACK_LEFT = 2, BACK_RIGHT = 3
		// 速度方向 由engineForce决定
		if (canShu.qian) {
			if (speed < -1)
				breakingForce = maxBreakingForce;
			else engineForce = maxEngineForce;
		}
		if (canShu.hou) {
			if (speed > 1)
				breakingForce = maxBreakingForce;
			else engineForce = -maxEngineForce / 2;
		}
		if (canShu.zuo) {
			if (vehicleSteering < steeringClamp)
				vehicleSteering += steeringIncrement;
		}
		else {
			if (canShu.you) {
				if (vehicleSteering > -steeringClamp)
					vehicleSteering -= steeringIncrement;
			}
			else {
				if (vehicleSteering < -steeringIncrement)
					vehicleSteering += steeringIncrement;
				else {
					if (vehicleSteering > steeringIncrement)
						vehicleSteering -= steeringIncrement;
					else {
						vehicleSteering = 0;
					}
				}
			}
		}
		// 刹车
		if (canShu.shouSha) breakingForce = maxBreakingForce;
		// 限速 100
		if (Math.abs(speed) > 100) breakingForce = maxBreakingForce;

		vehicle.applyEngineForce(engineForce, BACK_LEFT);
		vehicle.applyEngineForce(engineForce, BACK_RIGHT);

		vehicle.setBrake(breakingForce / 2, FRONT_LEFT);
		vehicle.setBrake(breakingForce / 2, FRONT_RIGHT);
		vehicle.setBrake(breakingForce, BACK_LEFT);
		vehicle.setBrake(breakingForce, BACK_RIGHT);

		vehicle.setSteeringValue(vehicleSteering, FRONT_LEFT);
		vehicle.setSteeringValue(vehicleSteering, FRONT_RIGHT);

		this.vehicleSteering = vehicleSteering
	}

	gengXinWuTi() {
		let che = this.che
		const transform = che.getChassisWorldTransform()
		const position = transform.getOrigin()
		const quaternion = transform.getRotation()
		let speed = che.getCurrentSpeedKmHour()

		let jieGuo = {
			bianHao: this.bianHao,
			mingZi: this.mingZi,
			cheSu: (speed < 0 ? '(R) ' : '') + Math.abs(speed).toFixed(1) + ' km/h',
			weiZhi: [position.x(), position.y(), position.z()],
			xuanZhuan: [quaternion.x(), quaternion.y(), quaternion.z(), quaternion.w()],
			lunZi: []
		}
		
		var tm, p, q, i;
		var n = che.getNumWheels();
		for (i = 0; i < n; i++) {
			che.updateWheelTransform(i, true);
			tm = che.getWheelTransformWS(i);
			p = tm.getOrigin();
			q = tm.getRotation();
			
			jieGuo.lunZi.push({
				weiZhi: [p.x(), p.y(), p.z()],
				xuanZhuan: [q.x(), q.y(), q.z(), q.w()]
			})
		}
		
		return jieGuo
	}

}

export {
	WuLiChe
}